#include "ExTranslator.h"
#include "DumpFuncAddress.h"
#include <stdio.h>

#define MAX_BUFF_SIZE  1024 * 6

void StackOverFlowLog(const char* str)
{
	SYSTEMTIME stTime = {0,};
	GetLocalTime(&stTime);

	char szLogFile[512] = {0x00,};
	::_snprintf(szLogFile, sizeof(szLogFile) -1, "%s %d-%d-%d.log", "sys/error", stTime.wYear, stTime.wMonth, stTime.wDay);
	FILE* fp	= ::fopen(szLogFile, "a+");
	if(fp)
	{
		::fprintf(fp,"%04d-%02d-%02d %02d:%02d:%02d.%03d\t",
			stTime.wYear,stTime.wMonth,stTime.wDay, stTime.wHour,stTime.wMinute,stTime.wSecond, stTime.wMilliseconds);

		fputs("Stack OverFlow ", fp);
		if(str)
			fputs(str, fp);
		else
			fputs("error string", fp);
		fputs("\n", fp);
		fflush(fp);	
		fclose(fp);
	}
}


void sbase::trans_func( unsigned int u, EXCEPTION_POINTERS* pExp ) 
{
	if (pExp->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW)
	{
		char* szBuf = new char[MAX_BUFF_SIZE];
		ZeroMemory(szBuf, MAX_BUFF_SIZE);
		DumpFuncAddress(10, szBuf,sizeof(szBuf)-1, pExp);
		StackOverFlowLog(szBuf);
		delete [] szBuf;
		
		return ;
	}

	throw Exception(pExp);
} 
 

